/*
* Conditions Of Use
*
* This software was developed by employees of the Sigmatrix(Beijing) Corporation.
* This software is provided by sigmatrix as a service and is expressly
* provided "AS IS."  Sigmatrix MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY.  Sigmatrix does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement.
*
*/
package com.sigmatrix.h5.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sigmatrix.h5.dao.InStorageDetailDao;
import com.sigmatrix.h5.dto.InStorageDetailData;
import com.sigmatrix.h5.dto.ProductData;
import com.sigmatrix.h5.entity.InStorageDetail;
import com.sigmatrix.h5.enums.ResponseEnum;
import com.sigmatrix.h5.exception.DaoException;
import com.sigmatrix.h5.exception.ServiceException;
import com.sigmatrix.h5.page.PageBean;
import com.sigmatrix.h5.response.Response;
import com.sigmatrix.h5.service.InStorageDetailService;
import com.sigmatrix.h5.service.ProductService;
import com.sigmatrix.h5.service.common.impl.BaseServiceImpl;
import com.sigmatrix.h5.web.vo.InStorageDetailVo;

/**
 * @ClassName: InStorageDetailServiceImpl
 * @Description: 入库单详情业务层接口
 * @author youjun 
 * @date 2017-07-06 14:21:21
 */
@Service(value = "inStorageDetailService")
public class InStorageDetailServiceImpl extends BaseServiceImpl<InStorageDetailData> implements InStorageDetailService {

	/** 入库单详情数据库操作接口 */
	@Autowired
	private transient InStorageDetailDao inStorageDetailDao;
	
	/**产品业务层接口*/
	@Autowired
	private transient ProductService productService;

	@Override
	public Response findListByPage(final PageBean<InStorageDetailData> pageBean,
			final InStorageDetailVo inStorageDetailVo, final Map<String, Object> map) throws ServiceException {

		try {
			final InStorageDetail inStorageDetail = new InStorageDetail();
			if (inStorageDetailVo != null) {
				BeanUtils.copyProperties(inStorageDetailVo, inStorageDetail);
			}
			final String enterpriseCode = inStorageDetailVo.getEnterpriseCode();
			final List<InStorageDetail> list = inStorageDetailDao.findByPage(pageBean.getPageNo(),
					pageBean.getPageSize(), inStorageDetail, enterpriseCode);
			final int total = inStorageDetailDao.getCountByCondition(inStorageDetail, enterpriseCode);
			final List<InStorageDetailData> dataList = convertDataList(list);
			convertPageBean(dataList, total, pageBean);
			return new Response(ResponseEnum.SUCCESS, pageBean);
		} catch (Exception e) {
			throw new ServiceException(e);
		}
	}

	/**
	 * @Title: convertDataList
	 * @Description: 转换实体bean和页面展示数据bean
	 * @return List<InStorageDetailData>
	 * @author youjun @date 2017-07-06 14:21:21
	 * @param list
	 * @return
	 * @throws ServiceException
	 */
	private List<InStorageDetailData> convertDataList(final List<InStorageDetail> list)
			throws ServiceException {

		final List<InStorageDetailData> dataList = new ArrayList<InStorageDetailData>();
		if (!CollectionUtils.isEmpty(list)) {
			InStorageDetailData data = null;
			for (final InStorageDetail item : list) {
				data = new InStorageDetailData();
				BeanUtils.copyProperties(item, data);

				dataList.add(data);
			}
		}
		return dataList;
	}


	@Override
	public List<InStorageDetailData> findListByCondition(final InStorageDetailVo inStorageDetailVo)
			throws ServiceException {

		final List<InStorageDetailData> dataList = new ArrayList<InStorageDetailData>();
		try {
			final InStorageDetail inStorageDetail = new InStorageDetail();
			if (inStorageDetailVo != null) {
				BeanUtils.copyProperties(inStorageDetailVo, inStorageDetail);
			}
			final List<InStorageDetail> list = inStorageDetailDao.findListByCondition(inStorageDetail, inStorageDetailVo.getEnterpriseCode());
			if (!CollectionUtils.isEmpty(list)) {
				InStorageDetailData data = null;
				for (final InStorageDetail item : list) {
					data = new InStorageDetailData();
					BeanUtils.copyProperties(item, data);
					dataList.add(data);
				}
			}
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
		return dataList;
	}

	/**
	 *@Title: fillProductInfo
	 *@Description: TODO()
	 *@return void
	 *@author youjun
	 *@date 2017年7月7日 下午9:25:37
	 *@param dataList
	 *@param enterpriseCode
	 * @throws ServiceException 
	 */
	@SuppressWarnings("unused")
	private void fillProductInfo(List<InStorageDetailData> dataList, String enterpriseCode) throws ServiceException {
		if(!CollectionUtils.isEmpty(dataList)) {
			ProductData product = null;
			final Map<Integer, ProductData> productMap = productService.getAllProductMap(enterpriseCode);
			for(final InStorageDetailData data:dataList) {
				product = productService.getProductFromMap(productMap, data.getProductId(), enterpriseCode);
				if(product!=null) {
					data.setProductName(product.getProductName());
					data.setPackStandard(product.getPackStandard());
//					data.setProductUnit(product.getProductUnit());
				}
			}
		}
	}

}
